package com.bhs.sansonglogistics.view;

import com.bhs.sansonglogistics.R;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RoundImageView extends androidx.appcompat.widget.AppCompatImageView {
    float radius = 180.0f;//20.0f；// 画出圆角效果，圆角（180度为正圆）

    public RoundImageView(Context context) {
        super(context);
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        //super(context, attrs);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        //获取圆角半径的大小
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
        radius = a.getInt(R.styleable.RoundImageView_radius,180);
        a.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        Path clipPath = new Path();
        int w = this.getWidth();
        int h = this.getHeight();
//向路径中添加圆角矩形，Path.Direction.CW表示顺时针方向
        clipPath.addRoundRect(new RectF(0, 0, w, h), radius, radius, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.onDraw(canvas);//修改画板
    }
}
